/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unifor.redes1.odirabel.gui.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author pedro
 */
public class ThreadAcceptClient extends Thread {

    private Socket accept;
    private JTextArea textArea;
    private String msg;
    private ClientesBuffer buffer;
    private BufferedReader input;
    private String userInput;
    private PrintWriter output;
    private boolean encerrou;
    private JScrollPane scrollPane;

    ThreadAcceptClient(Socket accept, JTextArea textArea, ClientesBuffer buffer, JScrollPane scrollPane) throws IOException {
        try {
            this.accept = accept;
            this.scrollPane = scrollPane;
            this.textArea = textArea;
            this.buffer = buffer;
            input = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            output = new PrintWriter(accept.getOutputStream(), true);
            encerrou = Boolean.FALSE;
        } catch (IOException ex) {
            throw ex;
        }


    }

    public void run() {

        while (!encerrou) {
            try {                
                userInput = input.readLine();
                tryMsg(userInput);
            } catch (IOException ex) {
                Logger.getLogger(ThreadAcceptClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }

    private void tryMsg(String msg) throws IOException {
        if (msg.contains("INFORM_USER")) {
            String user = msg.substring("INFORM_USER".length() + 1);
            textArea.append("Usuário " + user + " tentando se conectar.\n");
            output.println("REPLY_USER " + user);
            output.flush();
            buffer.add(new Cliente(user, this.accept.getInetAddress().getHostAddress()));

        } else if (msg.contains("REQUEST_USER_ALL")) {
            textArea.append("Usuário " + this.accept.getInetAddress().getHostAddress() + " solicitou usuários logados.\n");
            output.println("RESPONSE_USER_ALL " + buffer.listUsers());
            output.flush();
        } else if (msg.contains("DISCONNECT_USER")) {
            try {
                String user = msg.substring("DISCONNECT_USER".length() + 1);
                textArea.append("Usuário " + user + " solicitou desconexão.");
                buffer.removeUser(new Cliente(user, accept.getInetAddress().getHostAddress()));
                output.close();
                input.close();
                accept.close();
                encerrou = Boolean.TRUE;
            } catch (IOException ex) {
                throw ex;
            }
        }
        scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
        
    }
}
